package com.schillerchina.baselibrary.network.factory;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;


/**
 * @author zhangzhiguo
 * @date 2019-08-05 09:39
 * @Description: 网络请求GSON工厂类
 */

public class EncryptGsonConverterFactory extends Converter.Factory {
    private final Gson gson;
    private static final String TAG = "test";

    private EncryptGsonConverterFactory(Gson gson) {
        if (gson == null) {
            throw new NullPointerException("gson == null");
        }
        this.gson = gson;
    }

    public static EncryptGsonConverterFactory create() {
        return create(new Gson());
    }

    public static EncryptGsonConverterFactory create(Gson gson) {
        return new EncryptGsonConverterFactory(gson);
    }

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
                                                            Retrofit retrofit) {
        TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
        return new EncryptGsonResponseConverter<>(gson, adapter);
    }

    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[]
            parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
        return new EncryptGsonRequestConverter<>(gson, adapter);
    }

}
